package com.mpale.soa.filter;

import javax.servlet.*;
import java.io.IOException;

/**
 * 编码过滤器
 * @author lijian
 * @date 2020/6/17
 */
public class CharacterEncodingFilter implements Filter {

    /**
     * 项目启动时初始化
     */
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("过滤器初始化");
    }

    /**
     * 项目关闭时销毁
     */
    @Override
    public void destroy() {
        System.out.println("过滤器销毁");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        servletRequest.setCharacterEncoding("utf-8");
        servletResponse.setCharacterEncoding("utf-8");
        servletResponse.setContentType("text/html;charset=utf-8");

        System.out.println("过滤执行前...");
        filterChain.doFilter(servletRequest, servletResponse);
        System.out.println("过滤完成结果.");
    }
}
